"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : 配置文件权限异常恢复，主备切换
Create At   : 2020/07/27
@zou_jialiang0505328126
Description :
    1. 修改配置文件权限000
    2. 连接数据库
    3. 运行tpcc指令
    4. 恢复配置文件权限
    5. 连接数据库
    6. 主备切换
    7. 查看主备角色
    8. 连接数据库
    9. 新主节点运行tpcc指令
Expect      :
    1. 修改配置文件权限
    2. 连接数据库成功
    3. 运行tpcc成功
    4. 恢复配置文件权限成功
    5. 连接数据库成功
    6. 主备切换成功
    7. 查看主备角色成功
    8. 连接数据库成功
    9. 新主节点运行tpcc成功
History     :
    2020/07/23 created by @wan005
    2021/05/17 modified by @peilinqian 优化脚本逻辑,以及状态查询适配；
    2021/07/22 modified by @wan005 优化主备状态核对方法；
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class StopImmediate(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.log.info("----this is setup----")
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.sta1_root = Node(node='Standby1Root')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.constant = Constant()
        self.conf_file = os.path.join(macro.DB_INSTANCE_PATH,
                                      macro.DB_PG_CONFIG_NAME)
        self.log.info('----Opengauss_Reliability_Dbsys_Case172.py start----')
        self.log.info("----get socket file path----")
        time.sleep(30)
        sql = "show unix_socket_directory"
        file_path_msg = self.pri_sh.executDbSql(sql)
        self.log.info(file_path_msg)
        self.file_path = file_path_msg.splitlines()[2]
        self.log.info(self.file_path)

    def test_stop_immediate(self):
        self.log.info("----modify conf file permission----")
        chmod_cmd = f'chmod 000 {self.conf_file} ;' \
            f'ls -al {self.conf_file}'
        self.log.info(chmod_cmd)
        chmod_result = self.pri_root.sh(chmod_cmd).result()
        self.log.info(chmod_result)

        self.log.info("----connect opengauss----")
        no_socket_connect_msg = self.pri_sh.nosocketConnect()
        self.log.info(no_socket_connect_msg)
        self.assertEqual(no_socket_connect_msg, '')

        socket_connect_msg = self.pri_sh.socket_connect(self.file_path)
        self.log.info(socket_connect_msg)
        self.assertEqual(socket_connect_msg, '')

        self.log.info("----start TPCC----")
        tpcc_result = self.common.startTPCC(self.pri_root, macro.TPCC_PATH)
        self.log.info(tpcc_result)
        self.assertIn(self.constant.TPCC_SUCCESS_MSG, tpcc_result)

        self.log.info("----recover conf file permission----")
        chmod_cmd = f'chmod 700 {self.conf_file};' \
            f'ls -al {self.conf_file}'
        self.log.info(chmod_cmd)
        chmod_result = self.pri_root.sh(chmod_cmd).result()
        self.log.info(chmod_result)

        self.log.info("----connect opengauss----")
        no_socket_connect_msg = self.pri_sh.nosocketConnect()
        self.log.info(no_socket_connect_msg)
        self.assertEqual(no_socket_connect_msg, '')

        socket_connect_msg = self.pri_sh.socket_connect(self.file_path)
        self.log.info(socket_connect_msg)
        self.assertEqual(socket_connect_msg, '')

        self.log.info('校验刷脏页是否完成')
        dirty_page_finshed = self.pri_sh.check_whether_remain_dirty_page()
        self.assertTrue(dirty_page_finshed)

        self.log.info('校验备机是否完成数据同步')
        flag_1 = self.sta_1_sh.check_data_consistency()
        self.assertTrue(flag_1)
        flag_2 = self.sta_2_sh.check_data_consistency()
        self.assertTrue(flag_2)

        self.log.info("----switchover standby----")
        success_msg = self.constant.SWITCH_SUCCESS_MSG
        switchover_result = self.sta_1_sh.executeGsctl('switchover',
                                                       success_msg)
        self.assertTrue(switchover_result)
        refreshconf_result = self.sta_1_sh.exec_refresh_conf()
        self.assertTrue(refreshconf_result)

        self.log.info('----check opengauss status----')
        db_status = self.sta_1_sh.getDbClusterStatus('detail')
        self.log.info(db_status)
        self.assertIn(self.constant.PRIMARY_NORMAL, db_status)

        self.log.info("----connect opengauss----")
        no_socket_connect_msg = self.sta_1_sh.nosocketConnect()
        self.log.info(no_socket_connect_msg)
        self.assertEqual(no_socket_connect_msg, '')

        socket_connect_msg = self.sta_1_sh.socket_connect(self.file_path)
        self.log.info(socket_connect_msg)
        self.assertEqual(socket_connect_msg, '')

        self.log.info("----start TPCC (standby)----")
        tpcc_result = self.common.startTPCC(self.sta1_root, macro.TPCC_PATH)
        self.log.info(tpcc_result)
        self.assertIn(self.constant.TPCC_SUCCESS_MSG, tpcc_result)

        self.log.info('校验刷脏页是否完成')
        dirty_page_finshed = self.sta_1_sh.check_whether_remain_dirty_page()
        self.assertTrue(dirty_page_finshed)

        self.log.info('校验备机是否完成数据同步')
        flag_1 = self.pri_sh.check_data_consistency()
        self.assertTrue(flag_1)
        flag_2 = self.sta_2_sh.check_data_consistency()
        self.assertTrue(flag_2)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        self.log.info("----recover conf file permission----")
        chmod_cmd = f'chmod 700 {self.conf_file};' \
            f'ls -al {self.conf_file}'
        self.log.info(chmod_cmd)
        chmod_result = self.pri_root.sh(chmod_cmd).result()
        self.log.info(chmod_result)

        self.log.info("----switchover primary----")
        success_msg = self.constant.SWITCHOVER_SUCCESS_MSG
        result = self.pri_sh.executeGsctl('switchover', success_msg)
        self.log.info(result)
        result = self.pri_sh.exec_refresh_conf()
        self.log.info(result)

        self.log.info('----check opengauss status----')
        self.pri_sh.getDbClusterStatus('status')
